
package com.jf.cloud.service.sysmanager.dao.impl;

import javax.annotation.Resource;

import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.jdbc.RowMapperFactory;
import com.jf.cloud.service.sysmanager.dao.IPubgUserRealNameService;
import com.jf.cloud.service.sysmanager.entity.PubgUserRealName;


/**
 * 员工服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgUserRealNameService")
public class PubgUserRealNameServiceImpl extends BaseService<PubgUserRealName, Long> implements IPubgUserRealNameService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgUserRealNameServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	/**
	 * 新增或修改 实名认证信息
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 下午3:39:20
	 */
	@Override
	@Transactional
	public void saveOrUpdateUserRealNameInfo(PubgUserRealName pubgUserRealName) throws Exception {
		try {
			if (pubgUserRealName.getRowId() == null) {
				this.baseDao.saveAll(pubgUserRealName);
				return;
			}
			
			// 更新时 需要把上一个删除
			String sql = "DELETE FROM pubg_user_real_name WHERE CLUB_ID = ? AND USER_ID = ? AND AUDIT_STATE IN (10, 30) ";
			this.baseDao.safeUpdate(sql, 0, pubgUserRealName.getClubId(), pubgUserRealName.getUserId());
			
			// 保存对象
			this.baseDao.saveAll(pubgUserRealName);
		} catch (DataAccessException e) {
			throw new RuntimeException("新增或修改 实名认证信息出错！", e);
		}
	}

	/**
	 * 获取实名认证信息
	 *
	 * @author 朱建文 创建时间：2018年4月26日 下午4:03:46
	 */
	@Override
	public PubgUserRealName findPubgUserRealName(Long clubId, Long userId, Integer auditState) {
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" SELECT ");
			sql.append("   TEAM_ID, TEAM_NAME, USER_NAME, USER_ID_CARD, USER_ID_CARD_IMG, AUDIT_STATE ");
			sql.append(" FROM ");
			sql.append("   pubg_user_real_name ");
			sql.append(" WHERE ");
			sql.append("   CLUB_ID = ? AND USER_ID = ? ");
			sql.append(" ORDER BY APPLY_TIME DESC LIMIT 1");
			return this.baseDao.findObject(sql.toString(), 
					RowMapperFactory.newInstance(PubgUserRealName.class), clubId, userId);
		} catch (Exception e) {
			log.error("获取实名认证信息出错！", e);
		}
		return null;
	}

	/**
	 * 更新审核信息
	 * @author wr 创建时间：2018年4月28日 下午3:03:46
	 */
	@Override
	@Transactional
	public int updateUserState(Long rowId, Integer auditState,Long userId,SysStaff sysStaff) {
		//20通过 30拒绝
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" UPDATE pubg_user_real_name SET AUDIT_STATE = ?");
			sql.append(" ,AUDIT_ID = ?,AUDIT_ACCOUNT = ?,AUDIT_TIME = ?");
			sql.append(" WHERE ROW_ID = ?");
			int resultint = this.baseDao.update(sql.toString(),auditState,sysStaff.getRowId(),sysStaff.getStaffAccount(),DateUtil.getCurrentDateTime(),rowId);
			if(auditState.toString().equals("20") && resultint == 1){
				return this.baseDao.update("UPDATE pubg_user SET REAL_NAME_FLAG = ? WHERE ROW_ID = ?","1",userId);
			}
			return resultint;
		} catch (Exception e) {
			log.error("修改实名认证信息出错！", e);
		}
		return 0;
	}

}
